package org.cfx.springfestexplorer;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.webkit.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webview = findViewById(R.id.webview);
        WebView.setWebContentsDebuggingEnabled(true);
        WebSettings webSettings = webview.getSettings();
        //启用JavaScript
        webSettings.setJavaScriptEnabled(true);
        //启用DOM存储
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setDisplayZoomControls(false);
        webSettings.setUseWideViewPort(true);
//        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //根据URL的不同，选择使用WebView或浏览器打开网页
                if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("ftp")) {
                    view.loadUrl(url); //使用WebView打开
                    return true;
                } else if (url.startsWith("scheme://")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); //使用浏览器打开
                    startActivity(intent);
                    return true;
                } else {
                    return false;
                }
            }
        });
        webview.loadUrl("file:///android_asset/main.html"); //加载网页

        // 将 Java 对象添加为 JavaScript 接口
        webview.addJavascriptInterface(new MainActivity.MyJavaScriptInterface(), "Android");
    }

    // 定义一个 Java 对象，用于与 JavaScript 互动
    public class MyJavaScriptInterface {
        @JavascriptInterface
        public void changeToView(String url) {
            try {
                Intent intent = new Intent(MainActivity.this,WebViewActivity.class);
                intent.putExtra("url",url);
                // 在 Android 中弹出 Toast 消息
                System.out.println(url);
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}